package com.carsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.carsystem.entity.*;
import com.carsystem.mapper.CarMapper;
import com.carsystem.mapper.CarbrandMapper;
import com.carsystem.mapper.OpenrendMapper;
import com.carsystem.mapper.ShopMapper;
import com.carsystem.request.OpenRendRequestVo;
import com.carsystem.response.OpenRendResponseVo;
import com.carsystem.service.IOpenrendService;
import com.carsystem.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2021-12-04
 */
@Service
public class OpenrendServiceImpl extends ServiceImpl<OpenrendMapper, Openrend> implements IOpenrendService {

    @Resource
    private OpenrendMapper openrendMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private CarbrandMapper carbrandMapper;

    @Override
    public PageHandler selectOpenRendByPage(OpenRendRequestVo openRendRequestVo) {
        Map<String, Object> map = new HashMap<>();
        Integer offset = (openRendRequestVo.getPageIndex() - 1) * openRendRequestVo.getPageSize();
        map.put("offset",offset);
        map.put("pageSize", openRendRequestVo.getPageSize());
        if (StringUtils.isNotBlank(openRendRequestVo.getShopName())){
            map.put("shopName",openRendRequestVo.getShopName());
        }
        if (StringUtils.isNotBlank(openRendRequestVo.getCarBrandName())){
            map.put("carBrandName",openRendRequestVo.getCarBrandName());
        }
        if (StringUtils.isNotBlank(openRendRequestVo.getCarName())){
            map.put("carName",openRendRequestVo.getCarName());
        }
        if (StringUtils.isNotBlank(openRendRequestVo.getType())){
            map.put("type",openRendRequestVo.getType());
        }
        if (StringUtils.isNotBlank(openRendRequestVo.getStartDate())){
            map.put("startDate",openRendRequestVo.getStartDate());
        }
        if (StringUtils.isNotBlank(openRendRequestVo.getEndDate())){
            map.put("endDate",openRendRequestVo.getEndDate());
        }
        Integer count = openrendMapper.getOpenRendListCount(map);
        List<OpenRendResponseVo> openRendList = openrendMapper.getOpenRendList(map);
        PageHandler pageHandler = new PageHandler(openRendRequestVo.getPageIndex(), openRendRequestVo.getPageSize(),count);
        pageHandler.setList(openRendList);
        return pageHandler;
    }


    @Override
    public Result addOpenrend(Openrend openrend,int[] carbrandIds,int[] carIds,int[] shopIds) {
        if (carbrandIds!=null&&carbrandIds.length>0){
            for (int carbrandId : carbrandIds) {
                openrend.setCarbrandId(carbrandId);
            }
        }
        if (carIds!=null&&carIds.length>0){
            for (int carId : carIds) {
                openrend.setCarId(carId);
            }
        }
        if (shopIds!=null && shopIds.length>0){
            for (int shopId : shopIds) {
                openrend.setShopId(shopId);
            }
        }
        int insert = openrendMapper.insert(openrend);
        if (insert==1){
            return new Result(true,"发布信息成功",insert);
        }

        return new Result(false,"发布信息失败");
    }

    @Override
    public OpenRendVo getOpenRendById(int id) {
        Openrend openrend = openrendMapper.selectById(id);
        List<Integer> carList = new ArrayList<>();
        List<Integer> carbrandList = new ArrayList<>();
        List<Integer> shopList = new ArrayList<>();
        carList.add(openrend.getCarId());
        carbrandList.add(openrend.getCarbrandId());
        shopList.add(openrend.getShopId());
        OpenRendVo openRendVo = new OpenRendVo(openrend, carList, carbrandList, shopList);
        return openRendVo;

    }

    @Override
    public Result updateOpenrend(Openrend openrend, int[] carbrandIds, int[] carIds, int[] shopIds) {
        if (carbrandIds!=null&&carbrandIds.length>0){
            for (int carbrandId : carbrandIds) {
                openrend.setCarbrandId(carbrandId);
            }
        }
        if (carIds!=null&&carIds.length>0){
            for (int carId : carIds) {
                openrend.setCarId(carId);
            }
        }
        if (shopIds!=null && shopIds.length>0){
            for (int shopId : shopIds) {
                openrend.setShopId(shopId);
            }
        }
        int update = openrendMapper.updateById(openrend);
        if (update==1){
            return new Result(true,"修改信息成功",update);
        }

        return new Result(false,"修改信息失败");
    }

    @Override
    public Result deleteOpenrend(int id) {
        int i = openrendMapper.deleteById(id);
        if (i==1){
            return new Result(true,"删除信息成功");
        }

        return new Result(false,"删除信息失败");
    }

    @Override
    public OpenRendResponseVo selectOpenRentdById(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        OpenRendResponseVo openRendResponseVo = openrendMapper.selectOpenRentdById(map);
        return openRendResponseVo;
    }
}
